package cli.menu;

/**
 * BudgetBuoy
 * 
 * Simplify your personal expense management
 * 
 * @team cs2103jan13-w15-3j
 */

//--------------------------------------------------------------

/**
 * AddLoanPayment Class
 * 
 * Description: 
 * 
 * 
 * @package   cli.menu
 * @author    Lai Yong Quan (A0080909Y)
 * @edited by: Tan Si Ling (A0085314J)
 */



import shared.*;
import cli.tools.Process;


class AddLoanPayment extends Function{
	
	private static final String[] SELECTPM = {
		"-= Main menu > Add Entry > Loan Payment =-",
		"[b]: back [u]: undo [r]: redo [h]: home menu",
		"",
		"Select Payment Method: "	
	};
	
	private static final String[] SELECTLTP = {
		"-= Main menu > Add Entry > Loan Payment =-",
		"[b]: back [u]: undo [r]: redo [h]: home menu",
		"",
		"Select Loan to pay: "	
	};
	
	private static final String[] REQUESTINPUT = {
		"-= Main menu > Add Entry > Loan Payment =-",
		"[b]: back [u]: undo [r]: redo [h]: home menu",
		"Enter the information in the following format: ",
		"Name / Amount / Date (DD.MM.YYYY, optional) / Description (optional)",
		"",
		"Input: "
	};
	
	private static final String[] NOLOANS = {
	"\nThere are no Loans\n",
	"Returning to Add Entry menu\n"
	};
	
	private static State back = State.ADDENTRYMENU;
	private static State current = State.ADDLOANPAYMENT;
	
	
	
	public static State runMenu(){
		
		//Checks if there are loans to pay
		if (cli.getApi().getLoanList().isEmpty()) {
			cli.clearScreen();
			cli.print(NOLOANS);
			return back;
		}
		
		//cli.print(SELECTPM);
		
		Object choice = tools.selectFromList(cli.getApi().getModeList(), "Payment Mode",SELECTPM);
		cli.clearScreen();
		// if null object returned (back indicator)
		if (choice == null) {
			return back;
		}

		// if received Instruction object (divert to another menu)
		if (choice.getClass() == State.class) {
			return (State) choice;
		}

		// if choice is what was expected
		Mode paymentMode = (Mode) choice;

		//cli.print(SELECTLTP);
		
		choice = tools.selectFromList(cli.getApi().getLoanList(), "Loan", SELECTLTP);
		cli.clearScreen();
		// if back indicator received
		if (choice == null) {
			return back;
		}

		// if received Instruction object (divert to another menu)
		if (choice.getClass() == State.class) {
			return (State) choice;
		}

		// if choice is what was expected
		Loan loanToPay = (Loan) choice;
		
		String input = null;
		
		if(input == null || input.length() == 0){
		cli.print(REQUESTINPUT);
		input = cli.nextLine();
		cli.clearScreen();
		}
		
		if(input.length() == 1){
			char charInput = input.charAt(0);
		if(tools.isHotKey(charInput))
			return hotKeyFunction(charInput);
		}
		
		return normFunction(input, paymentMode, loanToPay );
		
	}
	
	
	private static State normFunction(String input, Mode paymentMode, Loan loanToPay){
		
		// process user input
		String[] processing = input.split("/");
		String name, price, details;
		details = "";
		int[] dmy = tools.initDate();
		// checks if input is valid : at least 2 "attributes" -
		// name and
		// price
		if (processing.length < 2){
			cli.print(INSUFFINFO);
			return current;
		}

		name = processing[0];
		price = processing[1];

		// need to see if 3rd entry is date/details
		String unknown;

		// convert money in string form to int
		int amount = tools.Money2int(price);
		if (amount <= 0){
			cli.println("Rejected input : Income <= 0");
			return State.ADDLOANPAYMENT;
		}
		
		if (processing.length >= 3) {
			unknown = processing[2];


			// checks if it is a date
			try {
				dmy = tools.processDate(unknown);
			} catch (Exception e) {
				// means it is not a valid date
				cli.print(INVALIDDATE);
				dmy = tools.initDate();
				// treat 3rd entry as details
				details = unknown + " ";
			}

			// checks if there is a details section
			// since user may use '/' characters, we will treat
			// anything
			// from
			// index 3 onwards as details
			if (processing.length >= 4) {
				for (int i = 3; i < processing.length; i++)
					details += processing[i];
			}

		}

		// now, input has been processed and treated to be
		// passed to
		// api
		
		try{
		Expense installment = cli.getApi().addLoanInstallment(name,
				amount, dmy[0], dmy[1], dmy[2], details, paymentMode,
				loanToPay);
		// Si Ling: **Keeping Track of the Actions DONE**
		tools.action.addToUndoStack((Object) null, installment,
				Process.ADDLOANINSTALLMENT, null);
		// **END**
		}
		catch(Exception e){
			cli.print(ERROR);
		}
		
		return current;
	
		
		
	}
	
	
	
	private static State hotKeyFunction(char charInput){
		State nextState = current;
		switch (charInput) {
		case 'b':
		case 'B':
			nextState = back;
		break;
		case 'u':
		case 'U':
			tools.undo();							
			break;
		case 'r':
		case 'R':
			tools.redo();
			break;
		case 'h':
		case 'H':
			nextState = State.MAINMENU;
			break;
		default:
			break;
		}
		
		return nextState;
	}
	
}